package com.gitee.feizns.explore.data_structure.array.easy;

/**
 * 605. 种花问题
 * @author feizns
 * @since 2020/3/23
 */
public class CanPlaceFlowers {

    public static void main(String[] args) {

    }

    public static boolean canPlaceFlowers(int[] flowerbed, int n) {
        int ret = 0;
        int cur = 0;
        boolean l = false;
        boolean r = false;
        for (int i = 0; i < flowerbed.length; i++) {
            if ( flowerbed[i] == 0 ) {
                cur++;
                if ( i == 0 )
                    l = true;
                else if ( i == flowerbed.length - 1 )
                    r = true;
            } else {
                if ( l )
                    ret += cur / 2;
                else
                    ret += (cur - 1) / 2;
                cur = 0;
                l = false;
            }
        }
        if ( r ) {
            if ( l )
                ret += (cur + 1) / 2;
            else
                ret += cur / 2;
        }
        return ret >= n;
    }

}
